import { message } from "@/utils/message";
import { queryCommunityUserDoorByUser, deleteUserDoor } from "@/api/sysele";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, computed, onMounted } from "vue";

export function useNotice() {
  const INITIAL_DATA = {
    id: "",
    openid: "",
    manageid: "",
    eid: "",
    floor: "",
    starttime: "",
    endtime: "",
    stung: "",
    sunit: "",
    roomid: "",
    housename: "",
    nickname: "", //扩展
    mobile: "", //扩展
    maxfloor: ""
  };

  const searchForm = reactive({
    nickname: "",
    mobile: ""
  });
  const dialogVisible = ref(false);
  const formData = ref({ ...INITIAL_DATA });

  const bmData = ref([]);

  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "用户昵称",
      prop: "nickname",
      minWidth: 130
    },
    {
      label: "手机号码",
      prop: "mobile",
      minWidth: 130
    },
    {
      label: "电梯信息",
      prop: "tung",
      minWidth: 130,
      formatter: row => {
        let item = "";
        if (row.tung) {
          item += row.tung + "栋";
        }
        if (row.unit) {
          item += row.unit + "单元";
        }
        if (row.elename) {
          item += "[" + row.elename + "]";
        }
        return item;
      }
    },
    {
      label: "申请楼层",
      prop: "floor",
      minWidth: 80,
      formatter: row => {
        return row.floor + "层";
      }
    },
    {
      label: "开始时间",
      prop: "starttime",
      minWidth: 130
    },
    {
      label: "结束时间",
      minWidth: 90,
      prop: "endtime"
    },
    {
      label: "业主信息",
      prop: "housename",
      minWidth: 130,
      formatter: row => {
        let item = "";
        if (row.stung) {
          item += row.stung + "栋";
        }
        if (row.sunit) {
          item += row.sunit + "单元";
        }
        if (row.roomid) {
          item += row.roomid + " ";
        }
        if (row.housename) {
          item += row.housename + " ";
        }
        return item;
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });

  function handleUpdate(row) {
    dialogVisible.value = true;
    formData.value = {
      ...row
    };
  }

  function handleDelete(row) {
    deleteUserDoor(row)
      .then(res => {
        if (res.success) {
          message("删除成功", { type: "success" });
          onSearch();
        } else {
          message("删除数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("删除数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    queryCommunityUserDoorByUser({
      nickname: searchForm.nickname,
      mobile: searchForm.mobile,
      pageSize: pagination.pageSize,
      currentPage: pagination.currentPage
    })
      .then(res => {
        if (res.success) {
          dataList.value = res.data.list;
          pagination.total = res.data.total;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    bmData,
    dialogVisible,
    formData,
    searchForm,
    loading,
    columns,
    dataList,
    pagination,
    buttonClass,
    onSearch,
    resetForm,
    handleUpdate,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
